﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Xml;
using System.IO;
using System.Windows.Resources;
 

namespace PSExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {           
            List<Cmdlet> strCommands = new List<Cmdlet>();
            lblMsg.Content = string.Empty;
            psStack.Children.Clear();
            try
            {
                if(txtCommand.Text.Trim().Length > 0)
                strCommands = GetData(txtCommand.Text, false);
            }
            catch (Exception ex)
            {
                LogHelper.log.Error(ex.Message);
            }

        }
        public List<Cmdlet> GetData(string strCommand, bool includeDetails)
        {
            List<Cmdlet> cmdLets = new List<Cmdlet>();
            try
            {
                XmlDocument xmlDoc = this.LoadXml();

                Cmdlet cmdLet = null;
                XmlNode xmlNode = null;
                Expander psCmdExpander = null;
                bool commandexists = false;
                    XmlNodeList cmdLetNodes = xmlDoc.SelectNodes(Constants.CmdLetNodeName_Cmd);
                    foreach (XmlNode cmdNode in cmdLetNodes)
                    {
                        psCmdExpander = new Expander();
                        psCmdExpander.Expanded += (o, e) =>
                        {
                            Expander temp = o as Expander;

                            Label lbl = temp.Header as Label;
                            if (lbl != null)
                            {
                                lbl.Background = new SolidColorBrush(Colors.LightGray);
                                lbl.Foreground = new SolidColorBrush(Colors.Black);
                            }
                            //temp.Background = new SolidColorBrush(Colors.Blue);
                        };
                        psCmdExpander.Collapsed += (o, e) =>
                            {
                                Expander temp = o as Expander;
                                Label lbl = temp.Header as Label;
                                if (lbl != null)
                                {
                                    lbl.Background = null;
                                    lbl.Foreground = new SolidColorBrush(Colors.LightGreen);
                                }
                                //temp.Background = null;
                            };
                        if (cmdNode.HasChildNodes)
                        {
                            if (cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Name).InnerText.ToLowerInvariant().Contains(strCommand.ToLowerInvariant()))
                            {
                                cmdLet = new Cmdlet();
                                xmlNode = cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Name);
                                if (xmlNode != null)
                                {
                                    commandexists = true;
                                   string cmdLetName = xmlNode.InnerText.Trim();
                                    psCmdExpander.Name = cmdLetName.Substring(cmdLetName.IndexOf('-')+1);
                                    //psCmdExpander.Header = xmlNode.InnerText.Trim();
                                    psCmdExpander.Header = new Label() { Content = xmlNode.InnerText.Trim(), Foreground= new SolidColorBrush(Colors.LightGreen) };                                    
                                    StackPanel stkPanel = new StackPanel();
                                    stkPanel.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EFEFFF"));
                                    Label lblcmdName = new Label();
                                    lblcmdName.Content = "Command Name:  " + cmdLetName;                                 
                                    xmlNode = cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Synopsis);
                                    Label lblSynopsis = new Label();
                                    if (xmlNode != null)
                                        lblSynopsis.Content = "Synopsis:  " + xmlNode.InnerText.Trim();
                                    stkPanel.Children.Add(lblcmdName);
                                    stkPanel.Children.Add(lblSynopsis);                                    
                                    xmlNode = cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Examples);
                                    if (xmlNode != null && xmlNode.HasChildNodes)
                                    {                                        
                                        XmlNodeList exampleNodes = xmlNode.ChildNodes;
                                        
                                        foreach (XmlNode exampleNode in exampleNodes)
                                        {
                                            Label lblExampleTitle = new Label();                                            
                                            Label lblExampleCode = new Label();                                            
                                            Label lblExampleRemarks = new Label();
                                            lblExampleTitle.Content = exampleNode.SelectSingleNode(Constants.CmdLetNodeName_Title).InnerText.Trim();
                                            lblExampleCode.Content = exampleNode.SelectSingleNode(Constants.CmdLetNodeName_Code).InnerText.Trim();
                                            lblExampleRemarks.Content = exampleNode.SelectSingleNode(Constants.CmdLetNodeName_Remarks).InnerText.Trim();
                                            stkPanel.Children.Add(lblExampleTitle);
                                            stkPanel.Children.Add(lblExampleCode);
                                            stkPanel.Children.Add(lblExampleRemarks); 
                                        }
                                    }
                                    xmlNode = cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Description);
                                    if (xmlNode != null)
                                    {
                                        Label lblDescription = new Label();
                                        lblDescription.Content = xmlNode.InnerText.Trim();
                                        stkPanel.Children.Add(lblDescription); 
                                    }

                                    xmlNode = cmdNode.SelectSingleNode(Constants.CmdLetNodeName_Syntax);
                                    if (xmlNode != null)
                                    {
                                        Label lblSyntax = new Label();
                                        lblSyntax.Content = xmlNode.InnerText.Trim();
                                        stkPanel.Children.Add(lblSyntax);                                         
                                    }
                                    psCmdExpander.Content = stkPanel;
                                    psCmdExpander.IsEnabled = true;
                                }                                        
                                psCmdExpander.UpdateLayout();
                                psStack.Children.Add(psCmdExpander);                                  
                            }
                        }
                       

                    }
                    if (!commandexists)
                    {
                        lblMsg.Content = "No commands with entered keyword";                        
                    }
                }            
            catch (Exception ex)
            {
                LogHelper.log.Error(ex);
            }
            return cmdLets;
        }


        private XmlDocument LoadXml()
        {
            XmlDocument xmlDoc = null;           
            xmlDoc = new XmlDocument();
            Uri XmlUri = new Uri("XML/SystemCommands.xml", UriKind.Relative);
            StreamResourceInfo XmlStreamSourceInfo = Application.GetResourceStream(XmlUri);
            xmlDoc.Load(XmlStreamSourceInfo.Stream);
            return xmlDoc;
        }
       
    }
}
